namespace DotNetNuke.Entities.Users
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Security;
    using DotNetNuke.Security.Roles;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using DotNetNuke.Services.Mail;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Web;
    using System.Web.Profile;
    using System.Web.Security;

    /// -----------------------------------------------------------------------------
    /// Project:    DotNetNuke
    /// Namespace:  DotNetNuke.Entities.Users
    /// Class:      UserController
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The UserController class provides Business Layer methods for Users
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	02/18/2004	documented
    /// [cnurse]    05/23/2005  made compatible with .NET 2.0
    /// </history>
    /// -----------------------------------------------------------------------------
    public class UserController
    {
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a User
        /// </summary>
        /// <remarks>This overload adds the User to the Membership Provider
        /// </remarks>
        /// <param name="objUser">The userInfo object to persist to the Database</param>
        /// <returns>The Id of the new User</returns>
        /// <history>
        /// [cnurse]	02/18/2005	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public int AddUser(UserInfo objUser)
        {
            return this.AddUser(objUser, true);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a User
        /// </summary>
        /// <remarks>This overload provides the option of adding the User to the Membership
        /// Provider
        /// </remarks>
        /// <param name="objUser">The userInfo object to persist to the Database</param>
        /// <param name="AddToMembershipProvider">A flag that determines whether the user is added
        /// to the Membership Provider</param>
        /// <returns>The Id of the new User</returns>
        /// <history>
        /// [cnurse]	02/18/2005	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public int AddUser(UserInfo objUser, bool AddToMembershipProvider)
        {
            int AddUser=0;
            int UserId = -1;
            try
            {
                UserRegistrationStatus Status;
                UserInfo objVerifyUser = this.GetUserByUsername(Null.NullInteger, objUser.Membership.Username);
                if (objVerifyUser != null)
                {
                    if (objVerifyUser.IsSuperUser)
                    {
                        Status = UserRegistrationStatus.UserAlreadyRegistered;
                    }
                    else
                    {
                        DotNetNuke.Common.Globals.SetApplicationName(objVerifyUser.PortalID);
                        if (Membership.ValidateUser(objUser.Membership.Username, objUser.Membership.Password))
                        {
                            UserId = objVerifyUser.UserID;
                            if (this.GetUserByUsername(objUser.PortalID, objUser.Membership.Username) != null)
                            {
                                Status = UserRegistrationStatus.UserAlreadyRegistered;
                            }
                            else
                            {
                                Status = UserRegistrationStatus.AddUser;
                            }
                        }
                        else
                        {
                            Status = UserRegistrationStatus.UsernameAlreadyExists;
                        }
                    }
                }
                else
                {
                    Status = UserRegistrationStatus.AddUser;
                }
                if (Status == UserRegistrationStatus.AddUser)
                {
                    PortalSecurity objSecurity = new PortalSecurity();
                    MembershipCreateStatus objStatus = MembershipCreateStatus.Success;
                    if (AddToMembershipProvider)
                    {
                        if (objUser.IsSuperUser)
                        {
                            DotNetNuke.Common.Globals.SetApplicationName(-1);
                        }
                        else
                        {
                            DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
                        }
                        MembershipUser objMembershipUser = Membership.CreateUser(objSecurity.InputFilter(objUser.Membership.Username, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objUser.Membership.Password, objSecurity.InputFilter(objUser.Membership.Email, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), null, null, objUser.Membership.Approved, out objStatus);
                    }
                    switch (((int) objStatus))
                    {
                        case 0:
                            UserId = DataProvider.Instance().AddUser(objUser.PortalID, objSecurity.InputFilter(objUser.Membership.Username, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(objUser.Profile.FirstName, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(objUser.Profile.LastName, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objUser.AffiliateID, objUser.IsSuperUser, objUser.Membership.Email);
                            if (((AddToMembershipProvider && (UserId != -1)) ? 1 : 0) != 0)
                            {
                                this.SaveProfileProperty(objUser.Membership.Username, objUser.Profile.ProfileProperties);
                                if (!objUser.IsSuperUser)
                                {
                                    RoleController objRoles = new RoleController();
                                    ArrayList arrRoles = CBO.FillCollection(DataProvider.Instance().GetPortalRoles(objUser.PortalID), typeof(RoleInfo));
                                    int refIntHelperL1 = arrRoles.Count - 1;
                                    for (int i = 0; i <= refIntHelperL1; i++)
                                    {
                                        RoleInfo objRole = (RoleInfo) arrRoles[i];
                                        if (objRole.AutoAssignment)
                                        {
                                            objRoles.AddUserRole(objUser.PortalID, UserId, objRole.RoleID, Null.NullDate);
                                        }
                                    }
                                }
                            }
                            break;

                        case 1:
                            return -1;

                        case 2:
                            return -2;

                        case 3:
                            return -3;

                        case 4:
                            return -4;

                        case 5:
                            return -5;

                        case 6:
                            return -6;

                        case 7:
                            return -7;

                        case 8:
                            return -8;

                        case 9:
                            return -9;

                        case 10:
                            return -10;

                        case 11:
                            return -11;
                    }
                }
                AddUser = UserId;
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
            }
            return AddUser;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes all users for a portal
        /// </summary>
        /// <param name="PortalId">Id of the portal</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	02/18/2005	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void DeleteAllUsers(int PortalId)
        {
            try
            {
                IEnumerator refObjectHelperL0=null;
                DotNetNuke.Common.Globals.SetApplicationName(PortalId);
                MembershipUserCollection objMembershipUsers = Membership.GetAllUsers();
                try
                {
                    refObjectHelperL0 = objMembershipUsers.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        MembershipUser objMembershipUser = (MembershipUser) refObjectHelperL0.Current;
                        UserInfo objUser = this.GetUserByUsername(PortalId, objMembershipUser.UserName);
                        if (objUser != null)
                        {
                            this.DeleteUser(objUser.PortalID, objUser.UserID, false, true);
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(PortalId);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes a user from a portal
        /// </summary>
        /// <param name="PortalId">Id of the portal</param>
        /// <param name="UserId">Id of the user</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	02/18/2005	Documented and modified to call helper method where
        /// the code was ported to.
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool DeleteUser(int PortalId, int UserId)
        {
            return this.DeleteUser(PortalId, UserId, true, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes a user from a portal
        /// </summary>
        /// <param name="PortalId">Id of the portal</param>
        /// <param name="UserId">Id of the user</param>
        /// <param name="notify">Flag to determine whether email notification is sent to the Administrator</param>
        /// <param name="deleteAdmin">Flag to determine whether to delete the Admin User</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	02/18/2005	moved code from Public overload
        /// [gve]       05/15/2005  moved SetApplicationName code just before Membership call
        /// </history>
        /// -----------------------------------------------------------------------------
        private bool DeleteUser(int PortalId, int UserId, bool notify, bool deleteAdmin)
        {
            bool DeleteUser=false;
            try
            {
                UserInfo objUser = this.GetUser(PortalId, UserId);
                bool CanDelete = true;
                IDataReader dr = DataProvider.Instance().GetPortal(PortalId);
                if (dr.Read() && (UserId == Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["AdministratorId"]))))
                {
                    CanDelete = deleteAdmin;
                }
                if (dr != null)
                {
                    dr.Close();
                }
                if (CanDelete)
                {
                    if (notify)
                    {
                        PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
                        DotNetNuke.Services.Mail.Mail.SendMail(_portalSettings.Email, _portalSettings.Email, "", DotNetNuke.Services.Localization.Localization.GetSystemMessage(_portalSettings, "EMAIL_USER_UNREGISTER_SUBJECT", objUser), DotNetNuke.Services.Localization.Localization.GetSystemMessage(_portalSettings, "EMAIL_USER_UNREGISTER_BODY", objUser), "", "", "", "", "", "");
                    }
                    if (objUser.IsSuperUser)
                    {
                        DotNetNuke.Common.Globals.SetApplicationName(-1);
                    }
                    else
                    {
                        DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
                    }
                    ProfileManager.DeleteProfile(objUser.Username);
                    Membership.DeleteUser(objUser.Username, true);
                    dr = DataProvider.Instance().GetRolesByUser(UserId, PortalId);
                    while (dr.Read())
                    {
                        DataProvider.Instance().DeleteUserRole(UserId, Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["RoleId"])));
                    }
                    dr.Close();
                    dr = DataProvider.Instance().GetUserByUsername(-1, objUser.Username);
                    dr.Read();
                    if (!dr.Read())
                    {
                        DataProvider.Instance().DeleteUser(UserId);
                    }
                    else
                    {
                        DataProvider.Instance().DeleteUserPortal(UserId, PortalId);
                    }
                    dr.Close();
                }
                DataCache.RemoveCache(this.GetCacheKey(objUser.PortalID, objUser.Username));
                DeleteUser = CanDelete;
            }
            catch (Exception exception1)
            {
                
                Exception Exc = exception1;
                object a = RuntimeHelpers.GetObjectValue(new object());
                
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(PortalId);
            }
            return DeleteUser;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes all the users from a portal
        /// </summary>
        /// <param name="PortalId">Id of the portal</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	02/18/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public void DeleteUsers(int PortalId)
        {
            ArrayList arr = new ArrayList();
            try
            {
                IEnumerator refObjectHelperL0=null;
                DotNetNuke.Common.Globals.SetApplicationName(PortalId);
                MembershipUserCollection objMembershipUsers = Membership.GetAllUsers();
                try
                {
                    refObjectHelperL0 = objMembershipUsers.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        MembershipUser objMembershipUser = (MembershipUser) refObjectHelperL0.Current;
                        if (!objMembershipUser.IsApproved | (DateTime.Compare(objMembershipUser.LastLoginDate, Null.NullDate) == 0))
                        {
                            UserInfo objUser = this.GetUserByUsername(PortalId, objMembershipUser.UserName);
                            arr.Add(objUser);
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
                int refIntHelperL0 = arr.Count - 1;
                for (int i = 0; i <= refIntHelperL0; i++)
                {
                    UserInfo objUser = (UserInfo) arr[i];
                    this.DeleteUser(objUser.PortalID, objUser.UserID);
                }
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(PortalId);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Builds a User Info object from a dataReader
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="dr">The datareader containing the user info</param>
        /// <param name="PortalId">Id of the portal</param>
        /// <returns>A UserInfo object</returns>
        /// <history>
        /// [cnurse]	02/22/2005	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        internal UserInfo FillUserInfo(IDataReader dr, int PortalId)
        {
            UserInfo FillUserInfo;
            try
            {
                UserInfo objUserInfo = new UserInfo();
                try
                {
                    objUserInfo.PortalID = PortalId;
                }
                catch (Exception exception1)
                {
                    
                    
                }
                try
                {
                    objUserInfo.IsSuperUser = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["IsSuperUser"]), objUserInfo.IsSuperUser)));
                }
                catch (Exception exception2)
                {
                    ProjectData.SetProjectError(exception2);
                    
                }
                if (objUserInfo.IsSuperUser)
                {
                    DotNetNuke.Common.Globals.SetApplicationName(-1);
                }
                else
                {
                    DotNetNuke.Common.Globals.SetApplicationName(PortalId);
                }
                try
                {
                    objUserInfo.UserID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["UserID"]), objUserInfo.UserID)));
                }
                catch (Exception exception3)
                {
                    ProjectData.SetProjectError(exception3);
                    
                }
                try
                {
                    objUserInfo.Username = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Username"]), objUserInfo.Username)));
                }
                catch (Exception exception4)
                {
                    ProjectData.SetProjectError(exception4);
                    
                }
                try
                {
                    objUserInfo.FirstName = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["FirstName"]), objUserInfo.FirstName)));
                }
                catch (Exception exception5)
                {
                    ProjectData.SetProjectError(exception5);
                    
                }
                try
                {
                    objUserInfo.LastName = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["LastName"]), objUserInfo.LastName)));
                }
                catch (Exception exception6)
                {
                    ProjectData.SetProjectError(exception6);
                    
                }
                try
                {
                    objUserInfo.AffiliateID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["AffiliateID"]), objUserInfo.AffiliateID)));
                }
                catch (Exception exception7)
                {
                    ProjectData.SetProjectError(exception7);
                    
                }
                objUserInfo.FullName = objUserInfo.FirstName + " " + objUserInfo.LastName;
                FillUserInfo = objUserInfo;
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(DotNetNuke.Common.Globals.GetApplicationName(PortalId));
            }
            return FillUserInfo;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Builds a User Info object from a dataReader
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="PortalId">Id of the portal</param>
        /// <param name="Username">The name of the user</param>
        /// <returns>A UserInfo object</returns>
        /// <history>
        /// [cnurse]	02/22/2005	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public UserInfo FillUserInfo(int PortalID, string Username)
        {
            IDataReader dr = DataProvider.Instance().GetUserByUsername(PortalID, Username);
            UserInfo retValue = null;
            try
            {
                if (dr.Read())
                {
                    retValue = this.FillUserInfo(dr, PortalID);
                }
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return retValue;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Builds a User Info object from a dataReader
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="dr">The datareader containing the user info</param>
        /// <param name="PortalId">Id of the portal</param>
        /// <param name="CheckForOpenDataReader">Flag to determine whether to chcek if the datareader is open</param>
        /// <param name="ProgressiveHydration">Flag to determine whether the membership and Profile
        /// objects are progressively hydrated</param>
        /// <returns>An ArrayList of UserInfo objects</returns>
        /// <history>
        /// [cnurse]	02/22/2005	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private UserInfo FillUserInfo(IDataReader dr, int PortalId, bool CheckForOpenDataReader, bool ProgressiveHydration)
        {
            UserInfo FillUserInfo;
            try
            {
                UserInfo objUserInfo = new UserInfo();
                bool bContinue = true;
                if (CheckForOpenDataReader)
                {
                    bContinue = false;
                    if (dr.Read())
                    {
                        bContinue = true;
                    }
                }
                if (bContinue)
                {
                    objUserInfo = this.FillUserInfo(dr, PortalId);
                    if (!ProgressiveHydration)
                    {
                        objUserInfo.Membership = this.FillUserMembership(objUserInfo);
                        objUserInfo.Profile = this.FillUserProfile(objUserInfo);
                    }
                }
                else
                {
                    objUserInfo = null;
                }
                FillUserInfo = objUserInfo;
            }
            finally
            {
                if (CheckForOpenDataReader && (dr != null))
                {
                    dr.Close();
                }
            }
            return FillUserInfo;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Builds a collection (ArrayList) of User Info objects from
        /// a dataReader
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="dr">The datareader containing the user info</param>
        /// <param name="PortalId">Id of the portal</param>
        /// <param name="ProgressiveHydration">Flag to determine whether the membership and Profile
        /// objects are progressively hydrated</param>
        /// <returns>An ArrayList of UserInfo objects</returns>
        /// <history>
        /// [cnurse]	02/22/2005	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private ArrayList FillUserInfoCollection(IDataReader dr, int PortalId, bool ProgressiveHydration)
        {
            ArrayList arr = new ArrayList();
            try
            {
                while (dr.Read())
                {
                    UserInfo obj = this.FillUserInfo(dr, PortalId, false, ProgressiveHydration);
                    arr.Add(obj);
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return arr;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Builds a User Info object's Membership Property
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="objUserInfo">The UserInfo objecto</param>
        /// <returns>A UserMembership object</returns>
        /// <history>
        /// [cnurse]	02/22/2005	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        internal UserMembership FillUserMembership(UserInfo objUserInfo)
        {
            UserMembership FillUserMembership;
            try
            {
                if (objUserInfo.IsSuperUser)
                {
                    DotNetNuke.Common.Globals.SetApplicationName(-1);
                }
                else
                {
                    DotNetNuke.Common.Globals.SetApplicationName(objUserInfo.PortalID);
                }
                MembershipUser objMembershipUser = Membership.GetUser(objUserInfo.Username);
                UserMembership u = new UserMembership();
                try
                {
                    u.Approved = objMembershipUser.IsApproved;
                }
                catch (Exception exception1)
                {
                    
                    
                }
                try
                {
                    u.LockedOut = objMembershipUser.IsLockedOut;
                }
                catch (Exception exception2)
                {
                    ProjectData.SetProjectError(exception2);
                    
                }
                try
                {
                    u.Username = objMembershipUser.UserName;
                }
                catch (Exception exception3)
                {
                    ProjectData.SetProjectError(exception3);
                    
                }
                try
                {
                    u.Password = objMembershipUser.GetPassword();
                }
                catch (Exception exception4)
                {
                    ProjectData.SetProjectError(exception4);
                    
                }
                try
                {
                    u.CreatedDate = objMembershipUser.CreationDate;
                }
                catch (Exception exception5)
                {
                    ProjectData.SetProjectError(exception5);
                    
                }
                try
                {
                    u.LastLoginDate = objMembershipUser.LastLoginDate;
                }
                catch (Exception exception6)
                {
                    ProjectData.SetProjectError(exception6);
                    
                }
                try
                {
                    u.Email = objMembershipUser.Email;
                }
                catch (Exception exception7)
                {
                    ProjectData.SetProjectError(exception7);
                    
                }
                try
                {
                    u.LastLockoutDate = objMembershipUser.LastLockoutDate;
                }
                catch (Exception exception8)
                {
                    ProjectData.SetProjectError(exception8);
                    
                }
                FillUserMembership = u;
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(DotNetNuke.Common.Globals.GetApplicationName(objUserInfo.PortalID));
            }
            return FillUserMembership;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Builds a User Info object's Profile Property
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="objUserInfo">The UserInfo objecto</param>
        /// <returns>A UserProfile object</returns>
        /// <history>
        /// [cnurse]	02/22/2005	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        internal UserProfile FillUserProfile(UserInfo objUserInfo)
        {
            UserProfile FillUserProfile;
            try
            {
                IEnumerator refObjectHelperL0=null;
                if (objUserInfo.IsSuperUser)
                {
                    DotNetNuke.Common.Globals.SetApplicationName(-1);
                }
                else
                {
                    DotNetNuke.Common.Globals.SetApplicationName(objUserInfo.PortalID);
                }
                ProfileBase objProfile = ProfileBase.Create(objUserInfo.Username, true);
                UserProfile u = new UserProfile();
                try
                {
                    refObjectHelperL0 = ProfileBase.Properties.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        SettingsProperty o = (SettingsProperty) refObjectHelperL0.Current;
                        u.ProfileProperties.Add(o.Name, RuntimeHelpers.GetObjectValue(objProfile[o.Name]));
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
                FillUserProfile = u;
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(DotNetNuke.Common.Globals.GetApplicationName(objUserInfo.PortalID));
            }
            return FillUserProfile;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the User's Cache Key
        /// </summary>
        /// <param name="PortalID">The Id of the Partal</param>
        /// <param name="Username">The name of the User</param>
        /// <returns>A String</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public string GetCacheKey(int PortalID, string Username)
        {
            return ("UserInfo|" + PortalID.ToString() + "|" + Username);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Get the current UserInfo object
        /// </summary>
        /// <returns>The current UserInfo</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static UserInfo GetCurrentUserInfo()
        {
            UserInfo objUser;
            if (HttpContext.Current == null)
            {
                if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
                {
                    PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
                    objUser = new UserController().GetUserByUsername(_portalSettings.PortalId, Thread.CurrentPrincipal.Identity.Name);
                    if (objUser != null)
                    {
                        return objUser;
                    }
                }
                return new UserInfo();
            }
            objUser = (UserInfo) HttpContext.Current.Items["UserInfo"];
            if (objUser != null)
            {
                return objUser;
            }
            return new UserInfo();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Retruns a String corresponding to the Registration Status of the User
        /// </summary>
        /// <param name="UserRegistrationStatus">The AspNetSecurity.MembershipCreateStatus</param>
        /// <returns>A String</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public string GetRegistrationStatus(MembershipCreateStatus UserRegistrationStatus)
        {
            switch (((int) UserRegistrationStatus))
            {
                case 1:
                    return DotNetNuke.Services.Localization.Localization.GetString("InvalidUserName");

                case 2:
                    return DotNetNuke.Services.Localization.Localization.GetString("InvalidPassword");

                case 3:
                    return DotNetNuke.Services.Localization.Localization.GetString("InvalidQuestion");

                case 4:
                    return DotNetNuke.Services.Localization.Localization.GetString("InvalidAnswer");

                case 5:
                    return DotNetNuke.Services.Localization.Localization.GetString("InvalidEmail");

                case 6:
                    return DotNetNuke.Services.Localization.Localization.GetString("UserNameExists");

                case 7:
                    return DotNetNuke.Services.Localization.Localization.GetString("UserEmailExists");

                case 8:
                    return DotNetNuke.Services.Localization.Localization.GetString("UserRejected");

                case 9:
                case 10:
                case 11:
                    return DotNetNuke.Services.Localization.Localization.GetString("RegError");
            }
            return null;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Returns all the SuperUsers
        /// </summary>
        /// <returns>An Arraylist of UserInfo objects</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetSuperUsers()
        {
            return this.FillUserInfoCollection(DataProvider.Instance().GetSuperUsers(), -1, true);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a single User
        /// </summary>
        /// <param name="PortalID">The Id of the portal</param>
        /// <param name="UserId">The Id of the user</param>
        /// <returns>A UserInfo object</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public UserInfo GetUser(int PortalId, int UserId)
        {
            IDataReader dr = DataProvider.Instance().GetUser(PortalId, UserId);
            UserInfo retValue = null;
            try
            {
                if (dr.Read())
                {
                    retValue = this.FillUserInfo(dr, PortalId);
                }
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return retValue;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a User by Name
        /// </summary>
        /// <param name="PortalID">The Id of the portal</param>
        /// <param name="Username">The username of the user</param>
        /// <returns>A UserInfo object</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public UserInfo GetUserByUsername(int PortalID, string Username)
        {
            return this.GetUserByUsername(PortalID, Username, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a User by Name
        /// </summary>
        /// <param name="PortalID">The Id of the portal</param>
        /// <param name="Username">The username of the user</param>
        /// <param name="SynchronizeUsers">Flag that indicates whether to synchronize the
        /// users before returning the collection</param>
        /// <returns>A UserInfo object</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public UserInfo GetUserByUsername(int PortalID, string Username, bool SynchronizeUsers)
        {
            UserInfo objUser = null;
            IDataReader dr = DataProvider.Instance().GetUserByUsername(PortalID, Username);
            try
            {
                if (SynchronizeUsers)
                {
                    this.SynchronizeUsers(PortalID);
                }
                if (!dr.Read())
                {
                    return objUser;
                }
                if (PortalID == -1)
                {
                    PortalID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["PortalID"]), PortalID)));
                }
                objUser = this.FillUserInfo(dr, PortalID);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return objUser;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// This overload returns all the Users
        /// </summary>
        /// <param name="SynchronizeUsers">Flag that indicates whether to synchronize the
        /// users before returning the collection</param>
        /// <param name="ProgressiveHydration">Flag that determines whether to use Progressive
        /// Hydration</param>
        /// <returns>An Arraylist of UserInfo objects</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetUsers(bool SynchronizeUsers, bool ProgressiveHydration)
        {
            return this.GetUsers(-1, SynchronizeUsers, ProgressiveHydration);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// This overload returns all the users of a Portal
        /// </summary>
        /// <param name="PortalId"></param>
        /// <param name="SynchronizeUsers"></param>
        /// <param name="ProgressiveHydration"></param>
        /// <returns>An Arraylist of UserInfo objects</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetUsers(int PortalId, bool SynchronizeUsers, bool ProgressiveHydration)
        {
            if (SynchronizeUsers)
            {
                this.SynchronizeUsers(PortalId);
            }
            return this.FillUserInfoCollection(DataProvider.Instance().GetUsers(PortalId), PortalId, ProgressiveHydration);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Saves Profile Propertis
        /// </summary>
        /// <param name="userName">the username</param>
        /// <param name="propHash">Hashtable of profile key/value pairs</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        private void SaveProfileProperty(string userName, Hashtable propHash)
        {
            IEnumerator refObjectHelperL0=null;
            PortalSecurity objSecurity = new PortalSecurity();
            ProfileBase objProfile = ProfileBase.Create(userName, true);
            try
            {
                refObjectHelperL0 = propHash.Keys.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    object key = RuntimeHelpers.GetObjectValue(refObjectHelperL0.Current);
                    if (ProfileBase.Properties[key.ToString()].PropertyType == typeof(string))
                    {
                        if (((propHash.ContainsKey(RuntimeHelpers.GetObjectValue(key)) && (propHash[RuntimeHelpers.GetObjectValue(key)] != null)) ? 1 : 0) != 0)
                        {
                            objProfile[key.ToString()] = objSecurity.InputFilter(propHash[RuntimeHelpers.GetObjectValue(key)].ToString(), PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup);
                        }
                        else
                        {
                            objProfile[key.ToString()] = Null.NullString;
                        }
                    }
                    else
                    {
                        objProfile[key.ToString()] = RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(propHash[RuntimeHelpers.GetObjectValue(key)]), ProfileBase.Properties[key.ToString()].PropertyType));
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            objProfile.Save();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Sets the password in the Membership Provider
        /// </summary>
        /// <param name="objUser">The use to update</param>
        /// <param name="newPassword">The new password</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	03/04/2005	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool SetPassword(UserInfo objUser, string newPassword)
        {
            return this.SetPassword(objUser, objUser.Membership.Password, newPassword);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Sets the password in the Membership Provider
        /// </summary>
        /// <param name="objUser">The use to update</param>
        /// <param name="oldPassword">The old password</param>
        /// <param name="newPassword">The new password</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	02/18/2005	Documented
        /// [cnurse]    03/03/2005  Modified to support SuperUsers
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool SetPassword(UserInfo objUser, string oldPassword, string newPassword)
        {
            bool retValue = false;
            try
            {
                if (objUser.IsSuperUser)
                {
                    DotNetNuke.Common.Globals.SetApplicationName(-1);
                }
                else
                {
                    DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
                }
                MembershipUser objMembershipUser = Membership.GetUser(objUser.Username);
                objMembershipUser.ChangePassword(oldPassword, newPassword);
                if (objMembershipUser.GetPassword() == newPassword)
                {
                    retValue = true;
                }
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
            }
            return retValue;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Synchronizes users between the DNN Users Table and the AspNet Users for a Portal
        /// </summary>
        /// <param name="PortalID">The Id of the Portal to synchronize</param>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public void SynchronizeUsers(int PortalID)
        {
            IEnumerator refObjectHelperL0=null;
            UserController objUserController = new UserController();
            MembershipUserCollection objMembers = Membership.GetAllUsers();
            try
            {
                refObjectHelperL0 = objMembers.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    MembershipUser objMember = (MembershipUser) refObjectHelperL0.Current;
                    if (objUserController.GetUserByUsername(PortalID, objMember.UserName) == null)
                    {
                        UserInfo objuser = new UserInfo();
                        objuser.PortalID = PortalID;
                        objuser.Membership.Username = objMember.UserName;
                        objuser.Membership.Approved = objMember.IsApproved;
                        objuser.AffiliateID = -1;
                        objUserController.AddUser(objuser, false);
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Unlock a User Account
        /// </summary>
        /// <param name="objUser">The User to unlock</param>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public void UnlockUserAccount(UserInfo objUser)
        {
            try
            {
                if (objUser.IsSuperUser)
                {
                    DotNetNuke.Common.Globals.SetApplicationName(-1);
                }
                else
                {
                    DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
                }
                Membership.GetUser(objUser.Membership.Username).UnlockUser();
                DataCache.RemoveCache(this.GetCacheKey(objUser.PortalID, objUser.Username));
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Updates a User
        /// </summary>
        /// <param name="objUser">The use to update</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	02/18/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public void UpdateUser(UserInfo objUser)
        {
            try
            {
                PortalSecurity objSecurity = new PortalSecurity();
                UserController objUserController = new UserController();
                DataProvider.Instance().UpdateUser(objUser.UserID, objSecurity.InputFilter(objUser.Profile.FirstName, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(objUser.Profile.LastName, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objUser.Membership.Email);
                if (objUser.IsSuperUser)
                {
                    DotNetNuke.Common.Globals.SetApplicationName(-1);
                }
                else
                {
                    DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
                }
                this.SaveProfileProperty(objUser.Membership.Username, objUser.Profile.ProfileProperties);
                MembershipUser objMembershipUser = Membership.GetUser(objUser.Membership.Username);
                objMembershipUser.Email = objSecurity.InputFilter(objUser.Membership.Email, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup);
                objMembershipUser.IsApproved = objUser.Membership.Approved;
                objMembershipUser.LastActivityDate = DateAndTime.Now;
                Membership.UpdateUser(objMembershipUser);
                if (objUser.Membership.Password != "")
                {
                    objMembershipUser.ChangePassword(objMembershipUser.GetPassword(), objUser.Membership.Password);
                }
                DataCache.RemoveCache(this.GetCacheKey(objUser.PortalID, objUser.Username));
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(objUser.PortalID);
            }
        }
    }
}

